# coding: utf-8
# 19-10-31 上午11:58

import copy
from collections import defaultdict

from main.common.decorators import decorate_cache
from main.models import Department, PositionConfig


@decorate_cache('get_dept_tree_key')
def get_dept_tree_info():
    data = Department.objects.filter(status=1).order_by('name').values()
    dept_tree = defaultdict(list)
    for item in data:
        dept_tree[item['parent_id']].append({'value': item['id'],
                                             'label': item['name'],
                                             })
    for key in copy.deepcopy(dept_tree):
        for ii, item in enumerate(dept_tree[key]):
            if dept_tree[item['value']]:
                dept_tree[key][ii]['children'] = dept_tree[item['value']]
    ret = [item for item in dept_tree[None]]
    return ret


# @decorate_cache('get_position_tree_key')
def get_position_tree_info():
    data = PositionConfig.objects.filter(status=1).order_by('name')
    ret = [
        {'label': item.name,
         'value': item.id}
        for item in data
    ]
    return ret
